package com.lengmou.service.impl;

import com.lengmou.dao.RechargeCardsDao;
import com.lengmou.entity.RechargeCards;
import com.lengmou.service.RechargeCardsService;
import com.lengmou.util.TimeTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Service("RechargeCardsService")
public class RechargeCardsServiceImpl implements RechargeCardsService {
    @Resource
    private RechargeCardsDao rcd;
    @Override
    public List<RechargeCards> getAll() {
        return rcd.querysAll();
    }

    @Override
    public List<RechargeCards> getCarsByState(int state) {
        return rcd.querysByState(state);
    }

    @Override
    public List<RechargeCards> getCardsByuserid(int userid) {
        return rcd.querysByUserid(userid);
    }

    @Override
    public List<RechargeCards> getCardsByMoney(double money) {
        return rcd.querysByMoney(money);
    }

    @Override
    public RechargeCards getCardsByid(String id) {
        return rcd.queryById(id);
    }

    @Override
    public int addCars(double money) {
        String time = TimeTool.time();
        UUID uuid = UUID.randomUUID();
        if(money>0){

            return rcd.addRechargeCards(new RechargeCards(uuid.toString(),money,0,0,time,null));
        }
//        表示金额必须大于0
        return -1;
    }

    @Override
    public int setCardsCreateTime(RechargeCards cards) {
        RechargeCards cardsByid = getCardsByid(cards.getId());
        if(cardsByid!=null){
            cardsByid.setCreatetime(cards.getCreatetime());
            return rcd.updateRechargeCards(cardsByid);
        }
        return -1;
    }

    @Override
    public int setCarsState(RechargeCards cards) {
        RechargeCards cardsByid = getCardsByid(cards.getId());
        if(cardsByid!=null){
            cardsByid.setState(cards.getState());
            return rcd.updateRechargeCards(cardsByid);
        }
//        根本找不到这张卡
        return -1;
    }

    @Override
    public int setCarsUseTime(RechargeCards cards) {
        RechargeCards cardsByid = getCardsByid(cards.getId());
        if(cardsByid!=null){
            cardsByid.setUsetime(cards.getUsetime());
            return rcd.updateRechargeCards(cardsByid);
        }
//        根本找不到这张卡
        return -1;
    }

    @Override
    public int setCarsUserid(RechargeCards cards) {
        RechargeCards cardsByid = getCardsByid(cards.getId());
        if(cardsByid!=null){
            cardsByid.setUserid(cards.getUserid());
            return rcd.updateRechargeCards(cardsByid);
        }
//        根本找不到这张卡
        return -1;
    }

    @Override
    public int setCarsMoney(RechargeCards cards) {
        RechargeCards cardsByid = getCardsByid(cards.getId());
        if(cardsByid!=null){
            cardsByid.setMoney(cards.getMoney());
            return rcd.updateRechargeCards(cardsByid);
        }
//        根本找不到这张卡
        return -1;
    }
//使用这张卡
    @Override
    public int useCarsMoney(String carid, int userid) {
        RechargeCards cardsByid = getCardsByid(carid);
        if(cardsByid!=null){
            cardsByid.setUserid(userid);
            cardsByid.setState(1);
            cardsByid.setUsetime(TimeTool.time());
            return rcd.updateRechargeCards(cardsByid);
        }
//        根本找不到这张卡
        return -1;
    }

    @Override
    public int addsCars(int num, double money) {
        if(money>0){
            List<RechargeCards> cardsList = new ArrayList<>();
            for (int i = 0; i < num; i++) {
                cardsList.add(new RechargeCards(UUID.randomUUID().toString(),money,0,0,TimeTool.time(),null));
            }
            return rcd.addsRechargeCards(cardsList);
        }
        return -1;
    }

    @Override
    public int deleteCars(String id) {
        RechargeCards cardsByid = getCardsByid(id);
        if(cardsByid!=null){
            return rcd.deleteRechargeCards(id);
        }
        return -1;
    }

    @Override
    public List<RechargeCards> search(Map<String, Object> map) {

        return rcd.search(map);
    }
}
